package com.gwt.boot.config;

import com.gwt.boot.constant.CommonEnum;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MyBatisFlexConfig implements ConfigurationCustomizer {
    private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql");
    public MyBatisFlexConfig(){
        //开启审计功能
        AuditManager.setAuditEnable(true);
        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(
                auditMessage -> logger.info(">>>>>>SQL：{};  {}ms", auditMessage.getFullSql().toLowerCase(), auditMessage.getElapsedTime())
        );
        //ID生成策略
        FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
        keyConfig.setKeyType(KeyType.Generator);
        //设置默认的id生成规则
        keyConfig.setValue(KeyGenerators.snowFlakeId);
        FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig();
        globalConfig.setKeyConfig(keyConfig);
        // 关闭banner
        globalConfig.setPrintBanner(false);
        //设置数据库正常时的值
        globalConfig.setNormalValueOfLogicDelete(CommonEnum.USED.getCode());
        //设置数据已被删除时的值
        globalConfig.setDeletedValueOfLogicDelete(CommonEnum.DELETE.getCode());
    }

    @Override
    public void customize(FlexConfiguration flexConfiguration) {

    }
}
